﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using System.ComponentModel;
using Foundation;

namespace Business {
   
    
    public class DataHandler : IDataHandler{
        private static DataHandler instance;
        private Guid currentSoundSuite;
        private IDataAccess data = DataAccess.DataAccess.GetInstance();


#if DEBUG 
        /// <summary>
        /// Setzt den zu verwendenden DataAccess
        /// </summary>
        public IDataAccess UsedDataLayer {
            get {
                return this.data;
            }
            set {
                this.data = value;
            }
        }

#endif 


        /// <summary>
        /// Gibt die aktuelle SoundSuite zurück, welche bearbeitet oder abgespielt werden kann
        /// </summary>
        public Guid CurrentSoundSuite {
            get {
                return this.currentSoundSuite;                 
            }
            set {
                this.currentSoundSuite = value;
                this.PropertyChanged(this, new PropertyChangedEventArgs("CurrentSoundSuite"));
            }
        }


        public static DataHandler GetInstance() {
            if (instance == null) {
                instance = new DataHandler();
            }
            return instance;
        }

        private DataHandler() {

        }
        
        
        /// <summary>
        /// Gibt eine List mit allen bereits vorhandenen categories zurück
        /// </summary>
        public List<Category> Categories {
            get {
                return data.GetAllCategories(); 
            }          
        }

        /// <summary>
        /// Erstellt eine Category anhand des Namens und übergibt sie an die DataAccess Schicht
        /// </summary>
        /// <param name="name"></param>
        public void SaveNewCategory(string name) {           
            Category c = new Category(name);
            if (!this.Categories.Contains(c)) {
                data.SaveNewCategory(c);
                if (PropertyChanged != null) {
                    PropertyChanged(this, new PropertyChangedEventArgs("categories"));
                }
            }
        }


        /// <summary>
        /// Event, welcher ausgelöst wird, wenn sich eine Eigenschaft geändert hat.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;


        public List<Track> UntaggedTracks {
            get {
                return data.GetUntaggedTracks(); 
            }
        }

        public void SaveCategory(Category c) {
            data.Save(c); 
            this.PropertyChanged(this, new PropertyChangedEventArgs("untaggedTracks")); 
        }


        public List<Track> TracksInCategory(Category cat) {
            return data.GetTracksByCategory(cat); 
        }


        public List<SoundSuite> SoundSuites {
            get { 
                return this.data.GetAllSoundSuites(); 
            }
        }


        public void CreateNewSoundSuite(string name) {
            data.CreateNewSoundSuite(name);
            this.PropertyChanged(this, new PropertyChangedEventArgs("SoundSuite")); 
        }


        public void DeleteSuite(SoundSuite soundSuite) {
            data.DeleteSuite(soundSuite);
            this.PropertyChanged(this, new PropertyChangedEventArgs("SoundSuite"));
        }





       

        public object[] GetCurrentEffectGroups() {
            return data.getEffektGroupsFromSoundSuiteID(this.currentSoundSuite); 
        }


        public SoundSuite GetCurrentSoundSuite() {
            if (!this.currentSoundSuite.Equals(Guid.Empty)) {
                return data.GetSoundSuiteByID(this.currentSoundSuite);
            }
            return null; 
        }


        /// <summary>
        /// Erstellt eine neue EffektGroup in der SoundSuite
        /// </summary>
        /// <param name="effektGroup"></param>
        public void AddEffektGroupToCurrentSoundSuite(EffektGroup effektGroup) {
            this.data.AddNewEffektGroup(effektGroup, this.currentSoundSuite);
            this.PropertyChanged(this, new PropertyChangedEventArgs("CurrentSoundSuite")); 
        }
    }
}
